package yibao.youyun.com.yibao.Adapter;


import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;

import com.squareup.picasso.Picasso;
import com.zanlabs.widget.infiniteviewpager.InfinitePagerAdapter;

import java.util.ArrayList;
import java.util.List;

import yibao.youyun.com.yibao.R;

/**
 * Created by woodnaonly on 2015/11/23.
 */
public class MockPagerAdapter extends InfinitePagerAdapter
{

    private final LayoutInflater mInflater;
    private final Context mContext;
    private List<Integer> mList;


    public void setDataList(List<Integer> list)
    {
        if (list == null || list.size() == 0)
            throw new IllegalArgumentException("list can not be null or has an empty size");
        this.mList = list;
        this.notifyDataSetChanged();
    }


    public MockPagerAdapter(Context context)
    {
        mContext = context;
        mInflater = LayoutInflater.from(mContext);
        mList = getViewPagerData();
    }


    @Override
    public View getView(int position, View view, ViewGroup container)
    {
        ViewHolder holder;
        if (view != null)
        {
            holder = (ViewHolder) view.getTag();
        } else
        {
            view = mInflater.inflate(R.layout.item_infinite_viewpager, container, false);
            holder = new ViewHolder(view);
            view.setTag(holder);
        }
        Integer item = mList.get(position);
//        holder.image.setImageResource(item);
        Picasso.with(mContext)
                .load(item)
                .placeholder(R.mipmap.ic_launcher)
                .resize(360, 200)
                .into(holder.image);
        return view;
    }


    @Override
    public int getItemCount()
    {
        return mList == null ? 0 : mList.size();
    }


    private static class ViewHolder
    {

        ImageView image;

        public ViewHolder(View view)
        {
            image = (ImageView) view.findViewById(R.id.item_image);
        }
    }

    public static List<Integer> getViewPagerData()
    {
        List<Integer> list = new ArrayList<>();
        Integer item1 = new Integer(R.drawable.item1);
        Integer item2 = new Integer(R.drawable.item2);
        Integer item3 = new Integer(R.drawable.item3);
        Integer item4 = new Integer(R.drawable.item4);
        Integer item5 = new Integer(R.drawable.item5);
        list.add(item1);
        list.add(item2);
        list.add(item3);
        list.add(item4);
        list.add(item5);
        return list;
    }

//    public static List<PagerItem> getViewPagerData()
//    {
//        List<PagerItem> list = new ArrayList<>();
//        PagerItem item = null;
//        for (int i = 0; i < 5; i++)
//        {
//            item = new PagerItem();
//            item.setDesc("Description:" + i);
//            item.setName("Name:" + i);
//            item.setPosition(i);
//            int index = i % 3;
//            if (index == 1)
//            {
//                item.setImageUrl("http://p3.123.sogoucdn.com/imgu/2015/09/20150924151622_720.jpg");
//            } else if (index == 2)
//            {
//                item.setImageUrl("http://p8.123.sogoucdn.com/imgu/2015/09/20150924151358_518.png");
//            } else
//            {
//                item.setImageUrl("http://p6.123.sogoucdn.com/imgu/2015/09/20150921184131_208.png");
//            }
//            list.add(item);
//        }
//        return list;
//    }

}
